import React, { Component } from 'react'
import './index.css'

export default class Child extends Component {
  getRandomIntInclusive(min, max) {
    min = Math.ceil(min)
    max = Math.floor(max)
    return Math.floor(Math.random() * (max - min + 1)) + min //含最大值，含最小值
  }
  // !#2 子组件调用父组件传递过来方法的同时并传参
  handleClick = () => {
    // 2 ~ 5
    const price = this.getRandomIntInclusive(2, 5)
    this.props.changePrice(this.props.id, price)
  }
  render() {
    const { name, price, info } = this.props
    return (
      <div className='child'>
        <h3 className='title'>标题：{name}</h3>
        <p className='price'>价格：{price.toFixed(2)}</p>
        <p className='product'>{info}</p>
        <button onClick={this.handleClick}>砍一刀</button>
      </div>
    )
  }
}
